package com.woniu.humanresource.controller;

import com.woniu.humanresource.entity.Contract;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.ContractService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
public class ContractController {

    @Resource
    ContractService contractService;
    //分页查询合同信息
    @PostMapping("/contract/{pageIndex}/{pageSize}")
    public ResponseText<PageBean<Contract>> getContracts(@PathVariable("pageIndex") int page,
                                                         @PathVariable("pageSize") int pageSize,
                                                         @RequestBody Contract contract){
        ResponseText<PageBean<Contract>> responseText=null;
        try {
            PageBean<Contract> pageBean = contractService.selectCondition(contract, page, pageSize);
            responseText=new ResponseText<PageBean<Contract>>(pageBean,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //添加合同信息
    @PostMapping("/contract")
    public ResponseText addContract(@RequestBody Contract contract){
        ResponseText responseText=null;
        try {
            contractService.insertContract(contract);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //修改合同信息
    @PutMapping("/contract")
    public ResponseText updateContract(@RequestBody Contract contract){
        ResponseText responseText=null;
        try {
            contractService.updateContract(contract);
            responseText=new ResponseText<PageBean<Contract>>(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //删除合同信息
    @DeleteMapping("/contract/{id}")
    public ResponseText deleteContract(@PathVariable("id") int id){
        ResponseText responseText=null;
        try {
            contractService.deleteContract(id);
            responseText=new ResponseText(null,200,"success");
        } catch (Exception e) {
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
